package com.jianganming.facade;

import javax.swing.*;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import java.awt.*;

public class WySpecialtyFacade {
    public static void main(String[] args) {
        JFrame f = new JFrame("外观模式: 婺源特产选择测试");
        Container cp = f.getContentPane();
        WySpecialty wys = new WySpecialty();
        JScrollPane treeView = new JScrollPane(wys.tree);
        JScrollPane scrollpane = new JScrollPane(wys.label);
        JSplitPane splitpane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, true, treeView, scrollpane); //分割面版
        splitpane.setDividerLocation(230);     //设置splitpane的分隔线位置
        splitpane.setOneTouchExpandable(true); //设置splitpane可以展开或收起
        cp.add(splitpane);
        f.setSize(650, 350);
        f.setVisible(true);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

class WySpecialty extends JPanel implements TreeSelectionListener {
    private static final long serialVersionUID = 1L;
    final JTree tree;
    JLabel label;
    private Specialty specialty1 = new Specialty("images/specialty1.jpg");
    private Specialty specialty2 = new Specialty("images/specialty2.jpg");
    private Specialty specialty3 = new Specialty("images/specialty3.jpg");
    private Specialty specialty4 = new Specialty("images/specialty4.jpg");
    private Specialty specialty5 = new Specialty("images/specialty5.jpg");
    private Specialty specialty6 = new Specialty("images/specialty6.jpg");
    private Specialty specialty7 = new Specialty("images/specialty7.jpg");
    private Specialty specialty8 = new Specialty("images/specialty8.jpg");

    public WySpecialty(){
        DefaultMutableTreeNode top = new DefaultMutableTreeNode("婺源特产");
        DefaultMutableTreeNode node1 = null, node2 = null, tempNode = null;
        node1 = new DefaultMutableTreeNode("婺源四大特产（红、绿、黑、白）");
        tempNode = new DefaultMutableTreeNode("婺源荷包红鲤鱼");
        node1.add(tempNode);
        tempNode = new DefaultMutableTreeNode("婺源绿茶");
        node1.add(tempNode);
        tempNode = new DefaultMutableTreeNode("婺源龙尾砚");
        node1.add(tempNode);
        tempNode = new DefaultMutableTreeNode("婺源江湾雪梨");
        node1.add(tempNode);
        top.add(node1);
        node2 = new DefaultMutableTreeNode("婺源其它土特产");
        tempNode = new DefaultMutableTreeNode("婺源酒糟鱼");
        node2.add(tempNode);
        tempNode = new DefaultMutableTreeNode("婺源糟米子糕");
        node2.add(tempNode);
        tempNode = new DefaultMutableTreeNode("婺源清明果");
        node2.add(tempNode);
        tempNode = new DefaultMutableTreeNode("婺源油煎灯");
        node2.add(tempNode);
        top.add(node2);
        tree = new JTree(top);
        tree.addTreeSelectionListener(this);
        label = new JLabel();
    }

    @Override
    public void valueChanged(TreeSelectionEvent e) {
        if (e.getSource() == tree) {
            DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
            if (node == null) return;
            if (node.isLeaf()) {
                Object object = node.getUserObject();
                String sele = object.toString();
                label.setText(sele);
                label.setHorizontalTextPosition(JLabel.CENTER);
                label.setVerticalTextPosition(JLabel.BOTTOM);
                sele = sele.substring(2, 4);
                if (sele.equalsIgnoreCase("荷包")) label.setIcon(specialty1);
                else if (sele.equalsIgnoreCase("绿茶")) label.setIcon(specialty2);
                else if (sele.equalsIgnoreCase("龙尾")) label.setIcon(specialty3);
                else if (sele.equalsIgnoreCase("江湾")) label.setIcon(specialty4);
                else if (sele.equalsIgnoreCase("酒糟")) label.setIcon(specialty5);
                else if (sele.equalsIgnoreCase("糟米")) label.setIcon(specialty6);
                else if (sele.equalsIgnoreCase("清明")) label.setIcon(specialty7);
                else if (sele.equalsIgnoreCase("油煎")) label.setIcon(specialty8);
                label.setHorizontalAlignment(JLabel.CENTER);
            }
        }
    }
}

class Specialty extends ImageIcon{

    public Specialty(String image){
        super(image);
    }
}
